package im.yangqiang.android.ui.widget;

import android.content.Context;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import im.yangqiang.android.ui.R;

/**
 * 挖宝，蛇形图
 * Created by Carlton on 2016/4/20.
 */
public class RewardLayout extends RelativeLayout
{
    private RewardView mView;

    public RewardLayout(Context context)
    {
        super(context);
        init(context, null, 0);
    }

    public RewardLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public RewardLayout(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr)
    {
        mView = new RewardView(context, attrs, defStyleAttr);
        mView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        addView(mView);
    }

    private List<PointEntity> mPointEntities = new ArrayList<>();

    /**
     * 添加贝塞尔曲线数据
     *
     * @param list
     */
    public void setDatum(List<PointF> list)
    {
        mView.clearPoints();
        mView.addPoints(list);
    }

    public RewardLayout addPoints(List<PointEntity> list)
    {
        mPointEntities.addAll(list);
        return this;
    }

    private int mKeyPointCount = 0;

    public void addPointsView(List<View> views)
    {
        mKeyPointCount = views.size();
        for (View view : views)
        {
            addView(view);
        }
        requestLayout();
    }

    public void clearPointsView()
    {
        removeAllViews();
        addView(mView);
    }

    public void addToView()
    {
        for (int i = 0; i < mPointEntities.size(); ++i)
        {
            ImageButton view = new ImageButton(getContext());
            PointEntity entity = mPointEntities.get(i);
            view.setImageDrawable(entity.getDrawable());
            view.setOnClickListener(entity.getListener());
            addView(view);
            view.bringToFront();
        }
    }

    /**
     * 返回有多少个关键点，关键点就是关卡点
     *
     * @return
     */
    public int getPointCount()
    {
        return mPointEntities.size() + mKeyPointCount;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        super.onLayout(changed, l, t, r, b);
        ArrayList<PointF> points = mView.getKeyPoints(getPointCount());
        for (int i = 1; i < getChildCount(); ++i)
        {
            View childAt = getChildAt(i);
            if (childAt instanceof RewardView || childAt instanceof ImageView)
            {
                continue;
            }
            PointF point = points.get(i - 1);
            final int start = (int) (mView.getPaddingLeft() + point.x);
            childAt.layout(start - childAt.getWidth() / 2, (int) point.y - childAt.getHeight() / 2, start + childAt.getWidth() / 2, (int) point.y + childAt.getHeight() / 2);
        }
        if (points.size() > mArrowIndex + 1 && mArrowIndex != -1)
        {
            addArrow(points.get(mArrowIndex), points.get(mArrowIndex + 1));
        }
    }

    /**
     * 设置箭头方向的 位置，从0开始算
     *
     * @param index
     */
    public void setArrowIndex(int index)
    {
        mArrowIndex = index;
    }

    private ImageView mArrowView;
    private int mArrowIndex = -1;

    public void addArrow(PointF point, PointF endPoint)
    {
        if (null == mArrowView)
        {
            mArrowView = new ImageView(getContext());
            mArrowView.setImageResource(R.drawable.ic_guess_arrow);
        }
        if (mArrowView.getParent() == null)
        {
            addView(mArrowView);
        }
        final float x = point.x + (endPoint.x - point.x) / 2;
        final float y = endPoint.y - point.y;


        float l = x;
        float t = (float) mView.getQuadY(l) - mArrowView.getMeasuredHeight() / 2;
        float r = l + mArrowView.getMeasuredWidth();
        float b = t + mArrowView.getMeasuredHeight();
        mArrowView.layout((int) l, (int) t, (int) r, (int) b);
        mArrowView.setRotation((float) mView.getAngel(l) + 90);
    }

    public static class PointEntity
    {
        private Drawable drawable;
        private OnClickListener listener;

        public PointEntity(Drawable drawable)
        {
            this.drawable = drawable;
        }

        public Drawable getDrawable()
        {
            return drawable;
        }

        public PointEntity(Drawable drawable, OnClickListener listener)
        {
            this.drawable = drawable;
            this.listener = listener;
        }

        public OnClickListener getListener()
        {
            return listener;
        }

        public void setListener(OnClickListener listener)
        {
            this.listener = listener;
        }
    }
}
